package Practice.com;

import java.util.Arrays;

public class PersonTset {
    private static Person[] per;
    public PersonTset() {
        per = new Person[3];
        per[0] =new Person("张三", 62, 174);
        per[1] =new Person("李四", 73, 182);
        per[2] =new Person("王五", 53, 168);
    }

    public void changeSort(Person[] per){
        for (int i = 0; i < per.length; i++) {
            int minindex = i;
            for (int j = i; j < per.length ; j++) {
                if (per[minindex].getWeight() > per[j].getWeight()) {
                    minindex = j;
                }
                Person temp = per[j];
                per[j] = per[minindex];
                per[minindex] = temp;
            }
        }
        for (int i = 0; i < per.length ; i++) {
            System.out.println(per[i]);
        }
    }

    public void quickSort(Person[] per,int left,int right){
        if (left >= right) {
            return;
        }
        int i = left;
        int j = right;
        int  jizhun = per[left].getHeight();
        while (i != j){
            while (j >  i && per[j].getHeight() >= jizhun) {
                j --;
            }
            while (j > i && per[i].getHeight() <= jizhun){
                i ++;
            }
            if (i < j) {
                Person person = per[i];
                per[i] = per[j];
                per[j] = person;
            }
        }
        if (per[i].getHeight() < jizhun){
            Person per1 = per[left];
            per[left] = per[i];
            per[i] = per1;
        }
        quickSort(per,left,i- 1);
        quickSort(per,i + 1, right);
    }
    public static void main(String[] args) {
       PersonTset pre = new PersonTset();
//       pre.changeSort();
//       pre.quickSort(per,0,per.length - 1);
//        pre.maoPAoSort(per);
//        pre.changeSort(per);
        pre.chaRuSort(per);
       System.out.println(Arrays.toString(per));
    }

    public void maoPAoSort(Person[] per){
        for (int i = 0; i < per.length - 1; i++) {
            for (int j = 0; j < per.length - i - 1; j++) {
                if (per[j].getHeight() > per[j + 1].getHeight()){
                    Person people = per[j];
                    per[j] = per[j + 1];
                    per[j + 1] = people;
                }
            }
        }
    }

    public void chaRuSort(Person[] per){
        for (int i = 0; i < per.length - 1; i++) {
            Person current = per[i +1];
            int index = i;
            for (; index >= 0 && current.getHeight() < per[index].getHeight(); index --){
                per[index + 1] = per[index];
            }
            per[index + 1] = current;
        }
    }
}

